package com.design.marketPrj.application.controller.product;

import com.alibaba.fastjson.JSONObject;
import com.design.marketPrj.application.controller.BaseController;
import com.design.marketPrj.application.service.ExcludeDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@RequestMapping("/productDetail")
@Controller
public class ProductDetailController extends BaseController {

    @Autowired
    ExcludeDataService excludeDataService;

    @RequestMapping("/index")
    public String getProductDetail(Model model, String code)
    {
        model.addAttribute("code",code);
        model.addAttribute("user",getUser());
        return "product/productDetail";
    }

    @ResponseBody
    @RequestMapping("/getData")
    public JSONObject getProductDetailData(Model model, String code, int type)
    {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH,-1);
        Date m = c.getTime();
        String mon = formatter.format(m);

        c.setTime(new Date());
        c.add(Calendar.DATE, - 7);
        Date d = c.getTime();
        String day = formatter.format(d);

        String startDate = type==1?day:mon;

        return excludeDataService.getProductDetail(code,startDate);
    }

}
